package tech.aistar.prj.service.Impl;

import org.apache.ibatis.session.SqlSession;
import tech.aistar.prj.entity.Cust;
import tech.aistar.prj.mapper.CustMapper;
import tech.aistar.prj.service.ICustService;
import tech.aistar.prj.util.MybatisUtil;

import java.util.List;

/**
 * 本类用于：
 *
 * @author 许华钢
 * @date 2020/9/26 19:22
 */
public class CustImpl implements ICustService {
    @Override
    public List<Cust> findAll() {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        CustMapper custMapper = sqlSession.getMapper(CustMapper.class);

        List<Cust> custs = custMapper.findAll();
        sqlSession.close();
        return custs;
    }

    @Override
    public int del(Integer id) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        CustMapper custMapper = sqlSession.getMapper(CustMapper.class);

        int count = custMapper.del(id);
//        int count = custMapper.deleteByPrimaryKey(id);
        sqlSession.commit();
        sqlSession.close();
        return count;
    }

    @Override
    public int insertCust(Cust cust) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        CustMapper custMapper = sqlSession.getMapper(CustMapper.class);
        int count = custMapper.insert(cust);
        sqlSession.commit();
        sqlSession.close();
        return count;
    }

    @Override
    public List<Cust> searchCont(String cname, Integer status) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        CustMapper custMapper = sqlSession.getMapper(CustMapper.class);

        List<Cust> custs = custMapper.searchCont(cname,status);
        sqlSession.close();
        return custs;
    }

    @Override
    public int updateByCust(Cust cust) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        CustMapper custMapper = sqlSession.getMapper(CustMapper.class);

        Cust custUpdate = new Cust();
        custUpdate.setCname(cust.getCname());
        custUpdate.setCpassword(cust.getCpassword());
        custUpdate.setCstatus(cust.getCstatus());
        custUpdate.setId(cust.getId());

        int count = custMapper.updateByCust(custUpdate);
        sqlSession.commit();
        sqlSession.close();
        return count;
    }
}
